package com.cj.mobile.iabp.mvp.ui.activity;

import static com.jess.arms.utils.Preconditions.checkNotNull;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.global.Constant;
import com.cj.mobile.iabp.app.utils.CommonUtils;
import com.cj.mobile.iabp.di.component.DaggerRoundsInfoComponent;
import com.cj.mobile.iabp.mvp.contract.RoundsInfoContract;
import com.cj.mobile.iabp.mvp.model.entity.RoundsInfo;
import com.cj.mobile.iabp.mvp.presenter.RoundsInfoPresenter;
import com.cj.mobile.iabp.mvp.ui.adapter.SignInImgListAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created on 2025/09/29 16:25
 * 查看查房信息
 *
 * @author Love_xie
 * module name is RoundsInfoActivity
 */
public class RoundsInfoActivity extends BaseActivity<RoundsInfoPresenter> implements RoundsInfoContract.View {
    /*----------------------------------------------------------------控件信息----------------------------------------------------------------*/
    @BindView(R.id.txvi_roundsinfo_code)
    TextView txviCode;                                                                              // 登记号
    @BindView(R.id.txvi_roundsinfo_name)
    TextView txviName;                                                                              // 患者姓名
    @BindView(R.id.txvi_roundsinfo_room)
    TextView txviWard;                                                                              // 查房时所在病房
    @BindView(R.id.txvi_roundsinfo_bed)
    TextView txviBunk;                                                                              // 查房时所在床位
    @BindView(R.id.txvi_roundsinfo_doctor)
    TextView txviWardRounds;                                                                        // 查房人员

    @BindView(R.id.txvi_roundsinfo_chest)
    TextView txviChest;                                                                              // 胸片位置
    @BindView(R.id.txvi_roundsinfo_chest_adjust)
    TextView txviXpzdlj;                                                                            // 胸片位置调整
    @BindView(R.id.txvi_roundsinfo_roundtime)
    TextView txviRoundTime;                                                                         // 查房时间
    @BindView(R.id.txvi_roundsinfo_effect)
    TextView txviFbxg;                                                                              // 反搏效果
    @BindView(R.id.txvi_roundsinfo_liquid)
    TextView txviGssl;                                                                              // 肝素水量
    @BindView(R.id.txvi_roundsinfo_pressure)
    TextView txviYldyl;                                                                             // 压力带压力
    @BindView(R.id.txvi_roundsinfo_gas)
    TextView txviHql;                                                                               // 氦气量

    @BindView(R.id.txvi_roundsinfo_azc)
    TextView txviAzc;
    @BindView(R.id.txvi_roundsinfo_dc)
    TextView txviDc;
    @BindView(R.id.txvi_roundsinfo_ecmo)
    TextView txviEcmo;                                                                              // ECMO时机
    @BindView(R.id.txvi_roundsinfo_machine)
    TextView txviMachineCode;                                                                       // 机器代码
    @BindView(R.id.txvi_roundsinfo_special)
    TextView txviSpecial;                                                                           // 特殊情况需详述
    @BindView(R.id.revi_roundsinfo_img)
    RecyclerView reviImgList;                                                                       // 特殊或异常情况拍照
    @BindView(R.id.txvi_roundsinfo_inspection)
    TextView txviInspection;                                                                        // 检验日期

    @BindView(R.id.txvi_roundsinfo_ast)
    TextView txviAst;                                                                               // AST
    @BindView(R.id.txvi_roundsinfo_alt)
    TextView txviAlt;                                                                               // ALT
    @BindView(R.id.txvi_roundsinfo_cr)
    TextView txviCr;                                                                                // 肌酐
    @BindView(R.id.txvi_roundsinfo_egfr)
    TextView txviEgfr;                                                                              // eGFR
    @BindView(R.id.txvi_roundsinfo_myo)
    TextView txviMyo;                                                                               // 肌红蛋白
    @BindView(R.id.txvi_roundsinfo_clac)
    TextView txviClac;                                                                              // 乳酸cLac
    @BindView(R.id.txvi_roundsinfo_ga)
    TextView txviMyog;                                                                              // 肌钙蛋白
    @BindView(R.id.txvi_roundsinfo_leftSize)
    TextView txviZsszmqnj;                                                                          // 左室舒张末期内径
    @BindView(R.id.txvi_roundsinfo_leftScore)
    TextView txviZssxfs;                                                                            // 左室射血分数

    /*----------------------------------------------------------------业务信息----------------------------------------------------------------*/

    // 数据源
    private RoundsInfo info;

    // 登记号
    private String code;
    // 患者姓名
    private String name;

    @Inject
    GridLayoutManager mGridLayoutManager;
    @Inject
    List<String> mRecyImgList;
    @Inject
    SignInImgListAdapter mImgAdapter;

    /**
     * 根据主题使用不同的颜色。
     * 如果想要纯透明，则需要重写此方法，返回值为 -1 即可。
     */
    public int useStatusBarColor() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(android.R.color.white, typedValue, true);
        return typedValue.data;
    }

    /**
     * 将状态栏改为浅色模式(状态栏 icon 和字体会变成深色)
     */
    public boolean useLightStatusBar() {
        return true;
    }


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerRoundsInfoComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_rounds_info;
    }

    public void getBundleValues(Bundle bundle) {
        if (bundle != null) {
            info = bundle.getParcelable("info");
            code = bundle.getString("code");
            name = bundle.getString("name");
        }
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle("查房信息");

        getBundleValues(getIntent().getExtras());
        initRecyclerView();

        loadData();
    }

    private void initRecyclerView() {
        ArmsUtils.configRecyclerView(reviImgList, mGridLayoutManager);
        reviImgList.setAdapter(mImgAdapter);
        mImgAdapter.setOnItemClickListener((view, viewType, data, position) -> {
            String val = (String) data;
            showImage(val);
        });
    }

    /**
     * 显示图片
     */
    public void showImage(String image) {
        if (!TextUtils.isEmpty(image)) {
            Intent intent = new Intent(this, PhotoViewActivity.class);
            intent.putExtra(Constant.IMAGE_URL, image);
            startActivity(intent);
        }
    }

    private void loadData() {
        if (info != null) {
            txviCode.setText(CommonUtils.isEmptyReturnStr(code, "暂无"));
            txviName.setText(CommonUtils.isEmptyReturnStr(name));
            txviWard.setText(CommonUtils.isEmptyReturnStr(info.getRoomCode()));
            txviBunk.setText(CommonUtils.isEmptyReturnStr(info.getBedCode()));
            txviWardRounds.setText(CommonUtils.isEmptyReturnStr(info.getDoctorName()));
            txviChest.setText(CommonUtils.isEmptyReturnStr(info.getPlace1()));
            txviXpzdlj.setText(CommonUtils.isEmptyReturnStr(info.getPlace2()));
            txviRoundTime.setText(CommonUtils.isEmptyReturnStr(info.getRoundTime()));
            txviFbxg.setText(CommonUtils.isEmptyReturnStr(info.getFbType()));
            txviGssl.setText(CommonUtils.isEmptyReturnStr(info.getGssType()));
            txviYldyl.setText(CommonUtils.isEmptyReturnStr(info.getYali()));
            txviHql.setText(CommonUtils.isEmptyReturnStr(info.getHaiqi()));
            txviAzc.setText(CommonUtils.isEmptyReturnStr(info.getLessL()));
            txviDc.setText(CommonUtils.isEmptyReturnStr(info.getLessR()));
            txviEcmo.setText(CommonUtils.isEmptyReturnStr(info.getEcmo()));
            txviMachineCode.setText(CommonUtils.isEmptyReturnStr(info.getMashine()));
            txviSpecial.setText(CommonUtils.isEmptyReturnStr(info.getSpecial()));
            txviInspection.setText(CommonUtils.isEmptyReturnStr(info.getCheckTime()));
            txviAst.setText(CommonUtils.isEmptyReturnStr(info.getAst()));
            txviAlt.setText(CommonUtils.isEmptyReturnStr(info.getAlt()));
            txviCr.setText(CommonUtils.isEmptyReturnStr(info.getCr()));
            txviEgfr.setText(CommonUtils.isEmptyReturnStr(info.getEgfr()));
            txviMyo.setText(CommonUtils.isEmptyReturnStr(info.getMyo()));
            txviClac.setText(CommonUtils.isEmptyReturnStr(info.getClac()));
            txviMyog.setText(CommonUtils.isEmptyReturnStr(info.getGa()));
            txviZsszmqnj.setText(CommonUtils.isEmptyReturnStr(info.getLeftSize()));
            txviZssxfs.setText(CommonUtils.isEmptyReturnStr(info.getLeftScore()));

            if(!TextUtils.isEmpty(info.getImages())){
                String[] path = info.getImages().split( ",");
                mRecyImgList.clear();
                Collections.addAll(mRecyImgList, path);
                mImgAdapter.notifyDataSetChanged();
            }

        }
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}